#include "ukeyactivationwidget.h"
#include "ui_ukeyactivationwidget.h"
#include <QGSettings>

UkeyActivationWidget::UkeyActivationWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::UkeyActivationWidget)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::CustomizeWindowHint | Qt::FramelessWindowHint);
    this->setProperty("useStyleWindowManager",false);
    ui->activationremindlabel->setVisible(false);

    m_thread=new activation_thread;
    connect(m_thread,&activation_thread::signal_ukey_activation,this,&UkeyActivationWidget::autoUkeyActivation);
    connect(m_thread,&activation_thread::finished,this,[=]{
        m_timer->stop();
        resetControlsState(-1);
        if(activationState==20) {
            ui->remindlabel->setText(tr("Ukey not recognized, please insert Ukey to activate."));
            ui->retrybtn->setEnabled(false);
        }

        if(flag == 0 && activationState != 20) { //激活状态锁，error 20:未检测到有效Ukey
            QString ret = tr("Ukey not inserted or invalid Ukey");
            ret = kylin_activation_get_result_message(activationState);
            if(ret.isEmpty())
                ret = tr("Ukey not inserted or invalid Ukey");

            if (activationState == 0) { // 激活成功
                ui->remindlabel->setText(tr("Ukey activated successfully!"));
                emit activationMessageSignal(activationState);
                flag = 0;

                emit activateSucceed();
            } else { // 激活失败
                ui->remindlabel->setText(ret);
                flag = 0;
            }
            exit_service();
        }
    });

    m_timer=new QTimer;
    connect(m_timer,&QTimer::timeout,this,[=]{
        static int cnt=0;

        Interface interface;
        QPixmap pixmap=interface.getThemeLoadingIcon(cnt);
        ui->activationiconlabel->setPixmap(pixmap);
        cnt++;
        if(cnt==7)
            cnt=0;
    });

    ukey_found_thread = new ukey_find_thread;
    connect(ukey_found_thread,&ukey_find_thread::ukey_ckeck_result,this,[=](int result) {
        if(0 == result) {
            if (!ui->retrybtn->isEnabled())
                ui->retrybtn->setEnabled(true);
        } else {
            if (ui->retrybtn->isEnabled())
                ui->retrybtn->setEnabled(false);
        }
    });
}

UkeyActivationWidget::~UkeyActivationWidget()
{
    if(m_thread->isRunning()) {
        m_thread->terminate();
        m_thread->wait();
    }
    if(ukey_found_thread->isRunning()) {
        ukey_found_thread->terminate();
        ukey_found_thread->wait();
    }
    delete m_thread;
    delete m_timer;
    delete ukey_found_thread;
    delete ui;
}

void UkeyActivationWidget::paintEvent(QPaintEvent *event)
{
    Interface interface;
    interface.setFontSize(ui->label);
}

void UkeyActivationWidget::on_returnbtn_clicked()
{
    exit_service();
    qDebug()<<"stop watch ukey event";
    if(ukey_found_thread->ukey_check_timer->isActive()) {
        ukey_found_thread->ukey_check_timer->stop();
    }
    emit returnSignal();
}

//自动激活
void UkeyActivationWidget::autoUkeyActivation(int activation)
{
    qDebug()<<"ukey activation state:"<<activation;
    this->activationState=activation;

//    if(m_thread->isRunning())
//        m_thread->terminate();

//    qDebug()<<"ukey activation status:"<<activation;

//    m_thread->terminate();
//    if(activation==20)
//    {
//        ui->remindlabel->setText(tr("Ukey not recognized, please insert Ukey to activate."));
//    }
//    if(flag == 0 && activation != 20) //激活状态锁，error 20:未检测到有效Ukey
//    {
//        QString ret = tr("Ukey not inserted or invalid Ukey");
//        ret = kylin_activation_get_result_message(activation);
//        if(ret.isEmpty())
//        {
//            ret = tr("Ukey not inserted or invalid Ukey");
//        }

//        if (activation == 0) // 激活成功
//        {
//            ui->remindlabel->setText(tr("Ukey activated successfully!"));
//            emit activationMessageSignal(activation);
//            flag = 1;

//            exit_service();
//            flag = 0;
//            emit activateSucceed();
//        }
//        else  // 激活失败
//        {
//            ui->remindlabel->setText(ret);
//            flag = 1;
//            flag = 0;
//        }
//    }
//    m_timer->stop();
//    resetControlsState(-1);
//    ui->retrybtn->setText(tr("Retry"));
}

void UkeyActivationWidget::on_retrybtn_clicked()
{
    exit_service();
    setRemindLabelText();
    resetControlsState(0);
    m_thread->start();
    m_timer->start(100);
}

void UkeyActivationWidget::resetControlsState(int type)
{
    if(type==0) {
        ui->activationiconlabel->setVisible(true);
        ui->activationremindlabel->setVisible(true);
        ui->retrybtn->setEnabled(false);
        ui->returnbtn->setEnabled(false);
    } else {
        ui->activationiconlabel->setVisible(false);
        ui->activationremindlabel->setVisible(false);
        ui->retrybtn->setEnabled(true);
        ui->returnbtn->setEnabled(true);
    }
}

void UkeyActivationWidget::setRemindLabelText()
{
    ui->remindlabel->setText(tr("please insert Ukey to activate."));
}

void UkeyActivationWidget::restartCheckUkey()
{
    qDebug()<<"start watch ukey event";
    if(!ukey_found_thread->ukey_check_timer->isActive()) {
        ukey_found_thread->ukey_check_timer->start(100);
    }
}
